<?
ob_start();//start buffering
require_once ROOT_PATH.'core/CGlobal.php';
require_once ROOT_PATH.'core/FunctionLib.php';
require_once ROOT_PATH.'core/Table.php';
require_once ROOT_PATH.'core/CacheLib.php';
require_once ROOT_PATH.'core/DB.php';

//load cached
CGlobal::$my_server	=	$server_list;
if(MEMCACHE_ON){
	CGlobal::$memcache_server=$memcache_server;
	require_once ROOT_PATH.'includes/memcache.class.php';
}

if(isset($_GET['page']) && $_GET['page']=='error'){
	define('ERROR_PAGE',1);
}
else{
	define('ERROR_PAGE',0);
}
if(isset($_REQUEST['trigger']) && (int)$_REQUEST['trigger']==1){
	exit();
}

//session
if(!defined('NO_SESSION') && !ERROR_PAGE){
	if(SESSION_TYPE == 'db'){
		//Session db store
		require_once ROOT_PATH.'includes/session.class.php';
	}
	elseif(SESSION_TYPE == 'memcache'){
		//Session memcache store
		require_once(ROOT_PATH."includes/memcache.session.php");
	}
	else{
		session_start();
	}
}

//load all configs
if(empty(CGlobal::$configs)){
	$update = isset($_SESSION['config_update_cached']) && $_SESSION['config_update_cached'];
	$_SESSION['config_update_cached'] = false;
	$r = DB::select(T_CONFIGS,false,false,false,__LINE__.__FILE__,86400*7,'configs/',$update);
	if(!empty($r)){
		foreach($r as $v){
			CGlobal::$configs[$v['conf_key']] = $v['conf_val'];
		}
	}
}

//load site configs
$site_configs = isset(CGlobal::$configs['site_configs']) ? unserialize(CGlobal::$configs['site_configs']) :
array(
	'site_name'		=>	'',
	'logo'			=>	'',
	'favicon'		=>	'',
	'currency'		=>	'VNĐ',
	'upload_size'	=>	1,
	'website_status'=>	'online'
);
CGlobal::$web_status = $site_configs['website_status'];

//load permission
if(isset(CGlobal::$configs['site_permission'])){
	CGlobal::$permission = unserialize(CGlobal::$configs['site_permission']);
	unset(CGlobal::$configs['site_permission']);
}

//load menu
if(isset(CGlobal::$configs['site_menu'])){
	CGlobal::$menu = unserialize(CGlobal::$configs['site_menu']);
	unset(CGlobal::$configs['site_menu']);
}


//load core lib
require_once ROOT_PATH.'core/Form.php';
require_once ROOT_PATH.'core/Url.php';
require_once ROOT_PATH.'core/User.php';
require_once ROOT_PATH.'includes/display.class.php';

//load other lib
require_once ROOT_PATH.'core/SmartyFunction.php';

global $display;
$display = new TplLoad();


if (get_magic_quotes_gpc()){
	function stripslashes_deep($value){
		$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
		return $value;
	}
	$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
	$_COOKIE  = array_map('stripslashes_deep', $_COOKIE);
}

CGlobal::$currency = $site_configs['currency'];
$cday = 86400;
CGlobal::$province = CacheLib::get('province',$cday);
CGlobal::$support = CacheLib::get('support',$cday);
CGlobal::$hotline = CacheLib::get('hotline',$cday);
CGlobal::$yahoo_support = CacheLib::get('yahoo',$cday);
if(empty(CGlobal::$province)){
	$res = DB::query("SELECT id, title, status, support, fullHotline, address FROM ".T_PROVINCE." ORDER BY position ASC, title ASC");
	while($row = @mysql_fetch_assoc($res)){
		if($row['status'] == 1){
			$support = array();
			$support = ($row['support'] != '') ? unserialize($row['support']) : $support ;
			if(!empty($support)){
				CGlobal::$hotline[$row['id']] = $support['hotline'];
				CGlobal::$yahoo_support[$row['id']] = $support['yahoo'];
				CGlobal::$support[$row['title']] = $support;
			}
			
		}else{
			unset($row['support']);
			unset($row['fullHotline']);
			unset($row['address']);
		}
		CGlobal::$province[$row['id']] = $row;
	}
	CacheLib::set('support', CGlobal::$support,$cday);
	CacheLib::set('province', CGlobal::$province,$cday);
	CacheLib::set('hotline', CGlobal::$hotline,$cday);
	CacheLib::set('yahoo', CGlobal::$yahoo_support,$cday);
}

//site info
CGlobal::$website_title = $site_configs['site_name'];
if(isset(CGlobal::$configs['site_keywords'])){
	CGlobal::$keywords  =  CGlobal::$configs['site_keywords'];
	unset(CGlobal::$configs['site_keywords']);
}
if(isset(CGlobal::$configs['site_description'])){
	CGlobal::$meta_desc = CGlobal::$configs['site_description'];
	unset(CGlobal::$configs['site_description']);
}

//unset set not use
unset($site_configs, $memcache_server, $server_list);
register_shutdown_function(array("DB","close"));
?>